//Include this file before any other extension's JS files

//gOneClickExtension - Main and the only global extension's variable
if (typeof gOneClickExtension == 'undefined')
{
    gOneClickExtension = function(){
        // this is a stub only
        throw Components.results.NS_ERROR_ABORT;
    };
}

//Branch with add-on preferences
gOneClickExtension.prefPrefixString = "extensions.oneclicksearch.";

//Load preferences and return them in single object with folowing fields:
//  - selectionType.       What modifier should be used. Values: "alt", "ctrl", "shift"
//  - enableMiddleButton.  Enable middle button click for search
//  - enableRegexp.        Use user-defined regexp for subject
//  - regexpMatch.         Regexp match. Used only if enableRegexp == true
//  - regexpReplace.       Regexp replace. Used only if enableRegexp == true
gOneClickExtension.LoadPreferences = function()
{
	var prefs = {};
	var branch = Components.classes["@mozilla.org/preferences-service;1"].
				 getService( Components.interfaces.nsIPrefService ).
				 getBranch( gOneClickExtension.prefPrefixString );

	prefs.selectionType      = branch.getCharPref( "selectionMethod"    );
    prefs.enableMiddleButton = branch.getBoolPref( "enableMiddleButton" );
	prefs.enableRegexp       = branch.getBoolPref( "enableRegexp"       );
	prefs.regexpMatch        = branch.getComplexValue( "regexpMatch",   Components.interfaces.nsISupportsString ).data;
	prefs.regexpReplace      = branch.getComplexValue( "regexpReplace", Components.interfaces.nsISupportsString ).data;
    //dump("OneClickSearch: LoadPreferences(): " + prefs.regexpReplace + "\n");

	return prefs;
}

//Save add-on's preferences from "prefs" variable. Format is same as in LoadPreferences()
gOneClickExtension.SavePreferences = function( prefs )
{
	var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);

	var branch = Components.classes["@mozilla.org/preferences-service;1"].
				 getService( Components.interfaces.nsIPrefService ).
				 getBranch( gOneClickExtension.prefPrefixString );

	branch.setCharPref( "selectionMethod",    prefs.selectionType      );
    branch.setBoolPref( "enableMiddleButton", prefs.enableMiddleButton );
	branch.setBoolPref( "enableRegexp",       prefs.enableRegexp       );

	str.data = prefs.regexpMatch;
	branch.setComplexValue( "regexpMatch", Components.interfaces.nsISupportsString, str );

	str.data = prefs.regexpReplace;
	branch.setComplexValue( "regexpReplace", Components.interfaces.nsISupportsString, str );
    //dump("OneClickSearch: SavePreferences(): " + str.data + "\n");

}